package com.wxmp.userwechat.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wxmp.userwechat.domain.SysUserNotifyEntity;
import com.wxmp.userwechat.domain.SysUserWechatEntity;
import com.wxmp.userwechat.mapper.SysUserNotifyMapper;
import com.wxmp.userwechat.mapper.SysUserWechatMapper;
import com.wxmp.userwechat.service.SysUserWechatService;

/**
 * 用户绑定微信
 * @author 王嵩
 */
@Component("sysUserWechatService")
public class SysUserWechatServiceImpl implements SysUserWechatService {

	@Autowired
	private SysUserWechatMapper sysUserWechatMapper;

	@Autowired
	private SysUserNotifyMapper sysUserNotifyMapper;


	@Override
	public int saveSysUserWechat(SysUserWechatEntity sysUserWechat) {
		int result = sysUserWechatMapper.save(sysUserWechat);
		sysUserWechatMapper.updateWechatStatus(String.valueOf(sysUserWechat.getUserId()), (short) 1);
		return result;
	}

	@Override
	public SysUserWechatEntity getSysUserWechatByOpenid(String openid) {
		SysUserWechatEntity sysUserWechat = sysUserWechatMapper.getObjectByOpenid(openid);
		return sysUserWechat;
	}

	@Override
	public SysUserWechatEntity getSysUserWechatByUserId(String userId) {
		SysUserWechatEntity sysUserWechat = sysUserWechatMapper.getObjectByUserId(userId);
		return sysUserWechat;
	}

	@Override
	public int updateSysUserWechat(SysUserWechatEntity sysUserWechat) {
		return sysUserWechatMapper.update(sysUserWechat);
	}

	@Override
	public int removeByUserId(String userId) {
		int count = sysUserWechatMapper.removeByUserId(userId);
		// 解绑微信时更新用户上的微信绑定状态
		sysUserWechatMapper.updateWechatStatus(userId, (short) 0);
		SysUserNotifyEntity sysUserNotifyEntity = sysUserNotifyMapper.getObjectById(userId);
		sysUserNotifyEntity.setWechatNotify((short) 0);
		// 解绑微信时更新用户发送消息设置为false
		sysUserNotifyMapper.update(sysUserNotifyEntity);
		return count;
	}

	@Override
	public Integer countByOpenid(String openid) {
		return sysUserWechatMapper.countByOpenid(openid);
	}
}
